package com.why.constantpool;

import com.sun.xml.internal.rngom.parse.host.Base;
import com.why.basictype.U2;
import com.why.basictype.U4;
import com.why.utils.BaseConversionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * @author by xiaoTian
 * @date 2022/5/21.
 */
public class ConstantUtf8Info extends ConstantPoolInfo{

    public short length;
    public byte[] bytes;
    public String bytesValue;

    public ConstantUtf8Info(byte tag){
        setTag(tag);
    }


    @Override
    public void read(InputStream inputStream) {
        U2 lengthU = U2.read(inputStream);//读取长度
        length = lengthU.getValue();
        bytes = new byte[this.length];//字符串的长度
        try {
            inputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //utf
        bytesValue = this.getValue();
    }

    public String getValue(){
        String ret = null;
        try {
            ret = new String(bytes,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public String toString() {
        return "ConstantUtf8Info{" +
                "length=" + length +
                ", bytesValue='" + bytesValue + '\'' +
                '}';
    }
}
